/**
 * Local Broadcast Manager 是广播局限在应用内，保持了程序的封闭和安全。
 * Local Broadcast 和 全局的Broadcast使用方法几乎一样
 * 只是注册时是向LocalBroadcastManager注册。
 * 发送广播时也是通过LocalBroadcastManger发送。
 * 唯一值得注意的是：Local Broadcast的Receive并不是只能接收Local Broadcast，它也能接收全局的Broadcast
 * @author 程文智
 */
package com.main.taxsystem.demo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;

import com.main.taxsystem.R;

public class LocalBroadcastManagerDemo extends Activity {
	private static final String ACTION = "com.main.taxsystem.demo.LocalBroadcastManagerDemo.HTTP_ACTION";
	private LocalBroadcastManager localBroadcastManager = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_local_broadcast_manager_demo);
		
		//注册Broadcast Receive
		localBroadcastManager = LocalBroadcastManager.getInstance(LocalBroadcastManagerDemo.this);
		localBroadcastManager.registerReceiver(new BroadcastReceiver(){
			@Override
			public void onReceive(Context context, Intent intent) {
				Toast.makeText(LocalBroadcastManagerDemo.this, "Receive a broadcast", Toast.LENGTH_LONG).show();
			}
		}, 
		new IntentFilter(ACTION));
		
		//运行异步线程
		new MyThread().start();
	}

	private class MyThread extends Thread{

		@Override
		public void run() {
			//TODO:add your code here
			localBroadcastManager.sendBroadcast(new Intent(ACTION));
		}
		
	}
}
